package TestServlets;
import TemplateParser.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author valiadam
 */
public class GetCurrentTimeServlet extends TemplateParsingServlet
{

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try
        {
            String strParsedTemplate = parseTemplateFile("HTML Templates/Misc/templGetCurrTime.html", request);
            if (strParsedTemplate != null)
            {
                out.print(strParsedTemplate);
            }
            else
            {
                out.print("Template parsing error");
            }
        } finally {
            out.close();
        }
    }

    // <editor-fold desc="Methods callable from template HTML">
    // These methods must have the prototype String method(HttpServletRequest request)
    // Add as many as you like :)

    public String getCurrentTime(Object request)
    {
        HttpServletRequest req = (HttpServletRequest) request;
        String strRemoteHost = req.getRemoteAddr();

        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd 'at' HH:mm:ss");
        return sdf.format(cal.getTime()) + ", dear " + strRemoteHost + "!";
    }

    // </editor-fold>
}
